package ControllerLayer;
import DomainLayer.*;
import java.util.ArrayList;

public class OrderController
{
    private CustomerController customerController;
    private OrderCollection orderCollection;
    private static OrderController instance;
    private UserCollection users;
    private ArrayList<OrderLine> list;
    private StockController stockController;
    private int orderIDcounter = 0;
   
    private OrderController()
    {
        customerController= CustomerController.getInstance();
        list = new ArrayList<OrderLine>();
        orderCollection=OrderCollection.getInstance();
        users=UserCollection.getInstance();
        stockController=StockController.getInstance();
        
    }
    
    //@return only one object of class OrderController.
    public static OrderController getInstance()
    {
        if(instance==null)
        {
            instance=new OrderController();
        }
        return instance;
    }
    
    /**
     * Creates new order object
     * 
     * @param id a unique order's ID.
     * @param idc the customer's ID.
     * @param ida the assistant's ID.
     */
    public Order createNewOrder(int id ,int idc, String ida)
    {
        Assistant assistant=(Assistant)users.findUser(ida);
        Customer customer=customerController.searchCustomer(idc);
        Order order=new Order(id,customer,assistant);
        orderCollection.addOrder(order);
       return order;
    }
    
    public void deleteOrder(int id)
    {
        orderCollection.deleteOrder(id);
    }
    
    
    public OrderLine addProductToOrderLine(Product product, int quantity)
    {
        OrderLine orderLine= new OrderLine(product,quantity);
        return orderLine;
    }
    
    public void addToListOrderLines(OrderLine orderline)
    {
        list.add(orderline);
        
    }
    
    public ArrayList<OrderLine> getList()
    {
        return list;
    }
    
    /**
     * Creates order and adds order lines with products in it.
     * Also the same order is added to the particular customer.
     */
    public void makeSale(int idc, String ida)
    {
        Order order= createNewOrder(getID(), idc,ida);
        
        for(OrderLine orderLine: list)
        {
            orderCollection.findOrder(getID()).addOrderLine(orderLine);
        }
        customerController.searchCustomer(idc).addOrder(order);
    }
    
    public void makeNewList()
    {
        list=new ArrayList<OrderLine>();
    }
    
    public String printInvoice(int id)
    {
        return orderCollection.findOrder(id).createInvoice();
    }
    
    /**
     * @return true if there are enough products in stock for customer's order and adds those products to order line.
     * @return false if there aren't enough products in the stock.
     */
    public boolean makeOrderLine(String barCode, int number)
    {
        if(stockController.reduceNumberOfProduct( barCode, number)){
            addToListOrderLines(addProductToOrderLine(stockController.searchItem(barCode).getProduct(),number));
            return true;
        }else{
            return false;
        }
    }
    
    public Items searchItem(String barCode){
        return stockController.searchItem(barCode);
    }
    
    public void raiseID(){
        orderIDcounter++;
    }
    
    public int getID(){
        return orderIDcounter;
    }
}
